﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.WorkFlow.Template
{
    /// <summary>
    /// 数据访问类WorkFlowRemindEventData。
    /// </summary>
    public class WorkFlowRemindEvent
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "RemindEventId", "WorkFlowId", "WorkTaskId", "Description", "OutTimeSms", "OutTimeMsg", "OutTimeEmail", "ReceiveSms", "ReceiveMsg", "ReceiveEmail", "OutTimeToUsers", "ReceiveToUsers" };
        private string tableName = "HF_WorkFlowRemindEvent";
        private string keyField = "WorkTaskId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(WorkFlowRemindEventEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@RemindEventId", entity.RemindEventId, typeof(string));
            sqlDataItem.AppendParameter("@WorkFlowId", entity.WorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@WorkTaskId", entity.WorkTaskId, typeof(string));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
            sqlDataItem.AppendParameter("@OutTimeSms", entity.OutTimeSms, typeof(bool));
            sqlDataItem.AppendParameter("@OutTimeMsg", entity.OutTimeMsg, typeof(bool));
            sqlDataItem.AppendParameter("@OutTimeEmail", entity.OutTimeEmail, typeof(bool));
            sqlDataItem.AppendParameter("@ReceiveSms", entity.ReceiveSms, typeof(bool));
            sqlDataItem.AppendParameter("@ReceiveMsg", entity.ReceiveMsg, typeof(bool));
            sqlDataItem.AppendParameter("@ReceiveEmail", entity.ReceiveEmail, typeof(bool));
            sqlDataItem.AppendParameter("@OutTimeToUsers", entity.OutTimeToUsers, typeof(string));
            sqlDataItem.AppendParameter("@ReceiveToUsers", entity.ReceiveToUsers, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public WorkFlowRemindEventEntity GetWorkFlowRemindEventEntity(DataRow dr)
        {
            WorkFlowRemindEventEntity entity = new WorkFlowRemindEventEntity();
            string value = "";
            entity.RemindEventId = dr["RemindEventId"].ToString();
            entity.WorkFlowId = dr["WorkFlowId"].ToString();
            entity.WorkTaskId = dr["WorkTaskId"].ToString();
            entity.Description = dr["Description"].ToString();
            value = dr["OutTimeSms"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.OutTimeSms = true;
                }
                else
                {
                    entity.OutTimeSms = false;
                }
            }
            value = dr["OutTimeMsg"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.OutTimeMsg = true;
                }
                else
                {
                    entity.OutTimeMsg = false;
                }
            }
            value = dr["OutTimeEmail"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.OutTimeEmail = true;
                }
                else
                {
                    entity.OutTimeEmail = false;
                }
            }
            value = dr["ReceiveSms"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.ReceiveSms = true;
                }
                else
                {
                    entity.ReceiveSms = false;
                }
            }
            value = dr["ReceiveMsg"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.ReceiveMsg = true;
                }
                else
                {
                    entity.ReceiveMsg = false;
                }
            }
            value = dr["ReceiveEmail"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.ReceiveEmail = true;
                }
                else
                {
                    entity.ReceiveEmail = false;
                }
            }
            entity.OutTimeToUsers = dr["OutTimeToUsers"].ToString();
            entity.ReceiveToUsers = dr["ReceiveToUsers"].ToString();
            return entity;
        }

        #endregion

        #region  构造函数
        public WorkFlowRemindEvent()
        { }
        public static WorkFlowRemindEvent GetInstance()
        {
            return new WorkFlowRemindEvent();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(WorkFlowRemindEventEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(WorkFlowRemindEventEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string workTaskId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, workTaskId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="remindEventId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string workTaskId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="RemindEventId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkFlowRemindEventTable(string workTaskId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="remindEventId">主键Id</param>
        /// <returns>DataTable</returns>
        public WorkFlowRemindEventEntity GetWorkFlowRemindEventEntity(string workTaskId)
        {
            DataTable dt = GetWorkFlowRemindEventTable(workTaskId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetWorkFlowRemindEventEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法
        /// <summary>
        /// 获得流程任务节点配置的所有事件
        /// </summary>
        /// <param name="workFlowId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetEventOfWorkFlow(string workFlowId)
        {
            string tmpSql = "select * from " + tableName + " where workFlowId=@workFlowId";
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@workFlowId", workFlowId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

